<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>优惠券管理 列表</title>
<jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<div class="Wrapper">
<div class="searchArea">
    <form id="searchBar">
        <div class="fieldset1 clearFix">

			<div class="FXBR">
				<div class="fieldBox">
					<div class="label">优惠券名称</div>
					<div class="field">
						<input name="cpNm" id="cpNm" type="text" class="ui-textbox" value="" ligerui="width: 160">
					</div>
				</div>
			</div>
            
            <div class="clearFix"></div>
	            <div class="btnsBox formBtns notRight">
					<span id="clearBtn" onclick="clearBtn();" class="sq-button-round">清空</span>
					<span id="searchBtn" onclick="searchBtn();" class="sq-button-round">查询</span>
					<span id="addBtn" onclick="addBtn();" class="sq-button-round">新增优惠券</span>
				</div>
        	</div>
        </form>
    </div>
</div>
<div id="dataGrid" class="sq-grid"></div>
</div>

<script type="text/javascript">
	var grid = null;
	$(function () {
	    initForm();
		initGrid();
	});

	//初始化表单
	function initForm(){
		$("#searchBar").ligerForm();
	}
	
	//查询按钮事件
	function searchBtn(){
		 var form = new liger.get("searchBar");
		 var data = form.getData();
		 for (name in data){
			grid.setParm(name,data[name]);
		 };
		grid.reload();
	}
	
	//查询参数
	function getSearchParams(){
		var form = new liger.get("searchBar");
        var data = form.getData();
        return data;
	}
	
	//初始化列表
	function initGrid(){
		$("#dataGrid").ligerGrid({
	    	url: '<%=path%>/mcr/mcrCouponMain/initList.do',
	    	method : 'post',
	    	isSingleCheck: false,
	    	parms:getSearchParams(),
	        columns: [
				{ display: '优惠券ID', name: 'id', width: 150, enabledSort:true},
				{ display: '优惠券名称', name: 'cpNm', width: 150, enabledSort:true},
				{ display: '优惠券类型', name: 'cpTypeNm', width: 150, enabledSort:true },
	            { display: '有效期', name: 'expireShow', width: 150, enabledSort:true },
				{ display: '创建日期', name: 'crtTm', width: 150, enabledSort:true, render:renderDate},
	            { display: '创建人', name: 'crtUsrName', width: 150, enabledSort:true},
				{ display: '状态', name: 'status', width: 150, enabledSort:true,
					render:function(rowData){
						if (rowData.status=="0") {
							return "待开启";
						} else if (rowData.status=="1") {
							return "已开启";
						}else {
							return "已停用";
						}
					}
				},
				getGridOperColumnContent()
	        ],
	        pageSize:10,
	        root:'rows',
	        record:'total',
	        hideLoadButton : true,
	        width: '100%',
	        height:'100%',
	        heightDiff:-2,
	        checkbox:true,
	        rownumbers:true
	    });
	    grid = liger.get("dataGrid");
	    grid.setHeight(100);
	}

	//Grid操作列
	function getGridOperColumnContent(){
		var data = {
			display: '操作',
			isSort: false,
			width: 280,
			render: function (rowdata, rowindex, value){

				var modify ='<a class="sq-button-hasicon" href="javascript:modifyRow(';
				modify +="'"+rowdata.id+"'";
				modify +=",'mid_man_grid_id'"
				modify +=")\"><i class=\"sqBtnIco_4\"></i>编辑</a>"

				var upload ='<a class="sq-button-hasicon" href="javascript:uploadTab(';
				upload +="'"+rowdata.id+"',";
				upload +="'"+'0'+"'";
				upload +=",'mid_man_grid_id'"
				upload +=")\"><i class=\"sqBtnIco_7\"></i>配置发放名单</a>"

				var status_1 ='<a class="sq-button-hasicon" href="javascript:upStatus(';
				status_1 +="'"+rowdata.id+"',";
				status_1 +="'"+'1'+"'";
				status_1 +=",'mid_man_grid_id'"
				status_1 +=")\"><i class=\"sqBtnIco_7\"></i>启用</a>"

				var status_2 ='<a class="sq-button-hasicon" href="javascript:upStatus(';
				status_2 +="'"+rowdata.id+"',";
				status_2 +="'"+'2'+"'";
				status_2 +=",'mid_man_grid_id'"
				status_2 +=")\"><i class=\"sqBtnIco_7\"></i>停用</a>"


				var returnStr = "";

				if(rowdata.canEdit == 1){
					returnStr += modify;
				}

				if(rowdata.canUpload == 1){
					returnStr += upload;
				}

				if(rowdata.status == 0){
					returnStr += status_1;
				}else if((rowdata.status == 1)){
					returnStr += status_2;
				}

				return returnStr;
			}
		}
		return data;
	}

	//响应回调
	function delCallback(msg){
		if(msg.success == 1){
			UIUtils.alertSuccess(msg.info);
			grid.reload();
		}else{
			UIUtils.alertError(msg.info);
		}
	}

	// 配置发放名单
	function uploadTab(_id){
		$.ligerDialog.SQopen({
			width: 800,
			height:400,
			title : '修改',
			url:'<%=path%>/mcr/mcrCouponMain/toImportPage.do?cpId='+_id,
			// 自定义buttons的时候 不要设置 fixButtons: true
			buttons: [{
				text: "保存", type: 'ok', cls: 'l-dialog-btn-ok',
				onclick: function (item, Dialog) {
					//在子页面里绑定 frameElement.dialog.onOk = function () {}
					if (Dialog.onOk) {
						Dialog.onOk();
					}
				}
			}, {
				text: "关闭", type: 'cancel', cls: 'l-dialog-btn-no',
				onclick: function (item, Dialog) {
					//在子页面里绑定 frameElement.dialog.onCancel = function () {}
					if (Dialog.onCancel) Dialog.onCancel();
				}
			}
			]
		});
	}

	// 修改优惠券状态
	function upStatus(_id, status){
		$.ligerDialog.confirm('确定修改状态吗?', function (flag) {
			if(flag == false)return;
			var url = '<%=path%>/mcr/mcrCouponMain/updStatus.do';
			AjaxUtils.ajaxPost(url, {cpId:_id, newStatus:status}, delCallback);
		});
	}

	//修改方法
	function modifyRow(_id){
		$.ligerDialog.SQopen({
			width: 800,
			height:400,
			title : '修改',
			url:'<%=path%>/mcr/mcrCouponMain/toModifyPage.do?cpId='+_id,
			// 自定义buttons的时候 不要设置 fixButtons: true
			buttons: [{
				text: "保存", type: 'ok', cls: 'l-dialog-btn-ok',
				onclick: function (item, Dialog) {
					//在子页面里绑定 frameElement.dialog.onOk = function () {}
					if (Dialog.onOk) {
						Dialog.onOk();
					}
				}
			}, {
				text: "关闭", type: 'cancel', cls: 'l-dialog-btn-no',
				onclick: function (item, Dialog) {
					//在子页面里绑定 frameElement.dialog.onCancel = function () {}
					if (Dialog.onCancel) Dialog.onCancel();
				}
			}
			]
		});
	}

	//新增
	function addBtn(){
		$.ligerDialog.SQopen({
			width: 800,
			height:400,
			title : '新增',
			url:'<%=path%>/mcr/mcrCouponMain/toAddPage.do',
			// 自定义buttons的时候 不要设置 fixButtons: true
			buttons: [{
				text: "保存", type: 'ok', cls: 'l-dialog-btn-ok',
				onclick: function (item, Dialog) {
					//在子页面里绑定 frameElement.dialog.onOk = function () {}
					if (Dialog.onOk) {
						Dialog.onOk();
					}
				}
			}, {
				text: "取消", type: 'cancel', cls: 'l-dialog-btn-no',
				onclick: function (item, Dialog) {
					//在子页面里绑定 frameElement.dialog.onCancel = function () {}
					if (Dialog.onCancel) {
						Dialog.onCancel()
					};
				}
			}
			]
		});
	}
	
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
		}
		return dateStr;
	}

	//清空按钮事件
	function clearBtn(){
		$("#resetBtn").click();  //重置 input-text
		var form = new liger.get("searchBar");
		form.setData({  //重置select等特殊表单
			"cpNm":""
		});
	}

</script>

</body>
</html>